package atomgit_hook

import (
	"encoding/json"
	"fmt"
	"testing"
)

var pushData = `
{
    "after": "35d168c828b15b09c63f9e860a53d289aedcfd9c",
    "before": "7a20078182acefdfe0b6570ddf9e628c124a8607",
    "commits": [
      {
        "added": [
          "src/api/events/atomgit.ts",
          "src/assets/allIcon/icon-atomgit.png"
        ],
        "author": {
          "email": "panleiming@linksaas.pro",
          "name": "panleiming",
          "username": "openlinksaas"
        },
        "committer": {
          "email": "panleiming@linksaas.pro",
          "name": "panleiming",
          "username": "openlinksaas"
        },
        "distinct": true,
        "id": "35d168c828b15b09c63f9e860a53d289aedcfd9c",
        "message": "add atomgit support\n",
        "modified": [
          "src-tauri/Cargo.toml",
          "src-tauri/src/events_decode.rs",
          "src/api/event_type.ts",
          "src/api/events.ts",
          "src/api/events/gitlab.ts",
          "src/api/events_subscribe.ts",
          "src/api/external_events.ts",
          "src/pages/Project/Access/common.ts",
          "src/pages/Project/Access/components/AccessTable.tsx",
          "src/pages/Project/Access/components/AddModal.tsx",
          "src/pages/Project/Access/index.tsx",
          "src/pages/Project/Record/SubscribeList.tsx",
          "src/pages/Project/Record/common.ts",
          "src/pages/Project/Record/components/CreateSubscribeModal.tsx",
          "src/pages/Project/Record/components/UpdateSubscribeModal.tsx",
          "src/pages/Project/Record/components/constants.ts",
          "src/pages/Project/Record/components/eventDesc.ts"
        ],
        "removed": [],
        "timestamp": "2023-09-10T12:29:00Z",
        "tree_id": "d348dbc312c5ad1f3c8fb5a76dce973c39325c69",
        "url": "https://atomgit.com/openlinksaas/desktop/commit/35d168c828b15b09c63f9e860a53d289aedcfd9c"
      }
    ],
    "compare": "https://atomgit.com/openlinksaas/desktop/compare?from=7a20078182acefdfe0b6570ddf9e628c124a8607&to=35d168c828b15b09c63f9e860a53d289aedcfd9c&tab=commit",
    "created": false,
    "deleted": false,
    "forced": false,
    "head_commit": {
      "added": [
        "src/api/events/atomgit.ts",
        "src/assets/allIcon/icon-atomgit.png"
      ],
      "author": {
        "email": "panleiming@linksaas.pro",
        "name": "panleiming",
        "username": "openlinksaas"
      },
      "committer": {
        "email": "panleiming@linksaas.pro",
        "name": "panleiming",
        "username": "openlinksaas"
      },
      "distinct": true,
      "id": "35d168c828b15b09c63f9e860a53d289aedcfd9c",
      "message": "add atomgit support\n",
      "modified": [
        "src-tauri/Cargo.toml",
        "src-tauri/src/events_decode.rs",
        "src/api/event_type.ts",
        "src/api/events.ts",
        "src/api/events/gitlab.ts",
        "src/api/events_subscribe.ts",
        "src/api/external_events.ts",
        "src/pages/Project/Access/common.ts",
        "src/pages/Project/Access/components/AccessTable.tsx",
        "src/pages/Project/Access/components/AddModal.tsx",
        "src/pages/Project/Access/index.tsx",
        "src/pages/Project/Record/SubscribeList.tsx",
        "src/pages/Project/Record/common.ts",
        "src/pages/Project/Record/components/CreateSubscribeModal.tsx",
        "src/pages/Project/Record/components/UpdateSubscribeModal.tsx",
        "src/pages/Project/Record/components/constants.ts",
        "src/pages/Project/Record/components/eventDesc.ts"
      ],
      "removed": [],
      "timestamp": "2023-09-10T12:29:00Z",
      "tree_id": "d348dbc312c5ad1f3c8fb5a76dce973c39325c69",
      "url": "https://atomgit.com/openlinksaas/desktop/commit/35d168c828b15b09c63f9e860a53d289aedcfd9c"
    },
    "ref": "refs/heads/develop",
    "repository": {
      "allow_forking": true,
      "archived": false,
      "clone_url": "https://atomgit.com/openlinksaas/desktop.git",
      "created_at": "2023-09-08T17:15:35Z",
      "default_branch": "develop",
      "description": "为软件研发团队连接先进的工具，知识和管理。",
      "disabled": false,
      "fork": false,
      "forks": 0,
      "forks_count": 0,
      "full_name": "openlinksaas/desktop",
      "git_url": "git://atomgit.com/openlinksaas/desktop.git",
      "homepage": "https://atomgit.com/openlinksaas/desktop",
      "html_url": "https://atomgit.com/openlinksaas/desktop",
      "id": 96897,
      "is_template": false,
      "master_branch": "develop",
      "name": "desktop",
      "owner": {
        "avatar_url": "https://file.atomgit.com/uploads/user/1694222084215_2176.png",
        "email": "panleiming@linksaas.pro",
        "html_url": "https://atomgit.com/openlinksaas",
        "id": "64fae54f49d9355e7a863eb6",
        "login": "openlinksaas",
        "name": "openlinksaas",
        "type": "User"
      },
      "private": false,
      "size": 181.236,
      "ssh_url": "git@atomgit.com:openlinksaas/desktop.git",
      "stargazers_count": 1,
      "updated_at": "2023-09-10T12:29:17Z",
      "url": "https://atomgit.com/openlinksaas/desktop",
      "visibility": "public"
    },
    "sender": {
      "avatar_url": "https://file.atomgit.com/uploads/user/1694222084215_2176.png",
      "html_url": "https://atomgit.com/openlinksaas",
      "id": "64fae54f49d9355e7a863eb6",
      "login": "openlinksaas",
      "type": "User"
    }
  }
`

func TestPushHook(t *testing.T) {
	pushHook := &PushHook{}
	err := json.Unmarshal([]byte(pushData), pushHook)
	if err != nil {
		t.Error(err)
		t.FailNow()
	}

	data, _ := json.MarshalIndent(pushHook.ToProtoBuf(), "  ", "  ")
	fmt.Println(string(data))
}
